package com.framework.modules.live.dao;

import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.framework.modules.live.entity.LiveHistoryEntity;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 *  
 * 
 * @author MIT
 * @email litmiss@gmail.com
 * @date 2020-11-03 16:19:13
 */
public interface LiveHistoryDao extends BaseMapper<LiveHistoryEntity> {

    @Select("select a.datestr,ifnull(b.popCount,0) as popCount,ifnull(b.giftCount,0) as giftCount,ifnull(b.onlineCount,0) as onlineCount,ifnull(b.totalSeconds,0) as totalSeconds " +
            "from ( " +
            "    SELECT date_sub(curdate(), interval 1 day) as datestr " +
            "    union all " +
            "    SELECT date_sub(curdate(), interval 2 day) as datestr " +
            "    union all " +
            "    SELECT date_sub(curdate(), interval 3 day) as datestr " +
            "    union all " +
            "    SELECT date_sub(curdate(), interval 4 day) as datestr " +
            "    union all " +
            "    SELECT date_sub(curdate(), interval 5 day) as datestr " +
            "    union all " +
            "    SELECT date_sub(curdate(), interval 6 day) as datestr " +
            "    union all " +
            " SELECT date_sub(curdate(), interval 7 day) as datestr   " +
            ") a left join ( " +
            "select uid,sum(pop_count) popCount,sum(gift_count) giftCount,sum(online_count) onlineCount,sum(total_seconds) totalSeconds,from_unixtime(adddate, '%Y-%m-%d') datestr from tb_live_history  where from_unixtime(adddate, '%Y-%m-%d') BETWEEN  DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND DATE_SUB(CURDATE(), INTERVAL 1 DAY) and uid=#{uid} " +
            " group by datestr " +
            ") b on a.datestr = b.datestr " +
            "ORDER BY a.datestr")
    List<Map<String,Object>> getStatLiveHistory(@Param("uid") Integer uid);

    List<Map<String,Object>> queryLiveHistory(Pagination page,@Param("params") Map<String, Object> params);
}
